/*
 * Copyrignt (c) qkier.com. All Rights Reserved.
 * Author: xuzeshui@qkier.com
 * Created Time: 2017-04-20 11:13:55
 * Last Modified: 2017-04-21 17:27:11
 * File Name: controllers/geo.go
 * Description:
 */

//
package controllers

import (
	"fmt"

	"openapi/errorcode"
	"openapi/geo/bdlbs"
	"openapi/geo/geoaddr"
	"openapi/geo/geohash"

	log "github.com/cihub/seelog"
)

type GEOController struct {
	BaseController
}

//通过地址转百度坐标
// @router /addr2location [get]
func (p *GEOController) Addr2Location() {
	defer p.Output("Addr2Location")

	addr := p.GetString("address", "")
	city := p.GetString("city", "")
	log.Infof("Addr2Location, addr:%s, city:%s", addr, city)

	req := &bdlbs.GeoAddr2LocationReq{addr, city}
	data := bdlbs.NewGeoAddr2LocationResp()
	if err := bdlbs.GetBDLBSService().Addr2Location(req, data); err == nil {
		if data.Code == 0 {
			p.Resp["code"] = errorcode.EC_GL_SUCCESS
			p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
			p.Resp["result"] = data.Result
		} else {
			p.Resp["code"] = errorcode.EC_GEO_ADDRESS2LOCATION_ERROR
			p.Resp["msg"] = fmt.Sprintf("LBSE:%d:%s", data.Code, data.Msg)
		}
	} else {
		p.Resp["code"] = errorcode.EC_GEO_ADDRESS2LOCATION_ERROR
		p.Resp["msg"] = err.Error()
	}
}

//通过坐标查询位置信息
// @router /location2addr [get]
func (p *GEOController) Location2Addr() {
	defer p.Output("Location2Addr")

	req := &bdlbs.GeoLocation2AddrReq{}
	req.Lat, _ = p.GetFloat("lat", 0.0)
	req.Lng, _ = p.GetFloat("lng", 0.0)
	req.CoordType = p.GetString("coordtype", "wgs84ll")

	resp := bdlbs.NewGeoLocation2AddrResp()
	if err := bdlbs.GetBDLBSService().Location2Addr(req, resp); err == nil {
		if resp.Code == 0 {
			p.Resp["code"] = errorcode.EC_GL_SUCCESS
			p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
			p.Resp["result"] = resp.Result
		} else {
			p.Resp["code"] = errorcode.EC_GEO_LOCATION2ADDR_ERROR
			p.Resp["msg"] = fmt.Sprintf("LBSE:%d:%s", resp.Code, resp.Msg)
		}
	} else {
		p.Resp["code"] = errorcode.EC_GEO_LOCATION2ADDR_ERROR
		p.Resp["msg"] = err.Error()
	}
}

//坐标转换
// @router /coordconv [get]
func (p *GEOController) CoordConv() {
	defer p.Output("CoordConv")

	lat, _ := p.GetFloat("lat", 0.0)
	lng, _ := p.GetFloat("lng", 0.0)
	from, _ := p.GetInt("from", 1)
	to, _ := p.GetInt("to", 5)

	coords := make([]*bdlbs.LocationItem, 0, 1)
	coords = append(coords, &bdlbs.LocationItem{Lat: lat, Lng: lng})

	if coordResult, err := bdlbs.GetBDLBSService().CoordConv(coords, from, to); err == nil {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["result"] = coordResult[0]
	} else {
		p.Resp["code"] = errorcode.EC_GEO_COORD_CONV_ERROR
		p.Resp["msg"] = err.Error()
	}
}

//GEOHash计算
// @router /geohash/encode [get]
func (p *GEOController) GeoHashEncode() {
	defer p.Output("GeoHashEncode")

	lat, _ := p.GetFloat("lat", 0.0)
	lng, _ := p.GetFloat("lng", 0.0)
	precision, _ := p.GetInt("precision", 9)
	hash, box := geohash.Encode(lat, lng, precision)
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	p.Resp["geohash"] = hash
	p.Resp["range_min_lat"] = box.MinLat
	p.Resp["range_max_lat"] = box.MaxLat
	p.Resp["range_min_lng"] = box.MinLng
	p.Resp["range_max_lng"] = box.MaxLng
}

//GEO定位信息
// @router /geohashaddr [get]
func (p *GEOController) GeoHashAddr() {
	defer p.Output("GeoHashAddr")
	lat, _ := p.GetFloat("lat", 0.0)
	lng, _ := p.GetFloat("lng", 0.0)
	coordType := p.GetString("coord_type", "wgs84ll")
	gitem := geoaddr.QueryGeoAddr(lat, lng, coordType)
	if gitem != nil {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = gitem
	} else {
		p.Resp["code"] = errorcode.EC_GEO_GEOADDR_ERROR
		p.Resp["msg"] = "geohash addr失败"
	}
}
